﻿@model Sharp.WebBooter.Areas.CMS.Models.MasterDataIndexModel

<section class="content-header">
    <h1>
        主数据设置
    </h1>
    <ol class="breadcrumb">
        <li><a href="@Url.Action("Index", "Home")"><i class="fa fa-home"></i> 首页</a></li>
        <li class="active">系统设置</li>
    </ol>
</section>
<section>
    <section class="vbox">
        <section class="scrollable padder p-t-sm">
            <div style="width: 25%; float: left; padding-right: 10px; height:100%">
                <ul id="tree" class="ztree" style="margin-top:0; width:100%;height:100%;background-color:white;"></ul>
            </div>
            <div style="width: 75%;height:100%; float: right;">
                <section class="panel panel-default remove-b">
                    <header class="panel-heading title">
                        主数据设置
                    </header>
                    <div class="wrapper-sm bg-black ribbon">
                        @Html.Bootstrap().LinkButton("添加", ToolBarIco.Create).Id("masterDataCreate").Click("javascript:alert('请选择分类');").Route(Url.Action("Create"))
                        @Html.Bootstrap().LinkButton("删除", ToolBarIco.Delete).Click("DeleteMasterData('example','" + @Url.Action("Delete") + "')").Route(Url.Action("Delete"))
                    </div>
                    <div class="table-responsive">
                        <table id="example" width="100%" class="table table-bordered table-striped table-hover dataTable text-center"></table>
                    </div>
                </section>
            </div>
        </section>
    </section>
</section>
<input id="assemblyQualifiedName" type="hidden" value="" />


<script type="text/javascript">
    var setting = {
        view: {
            dblClickExpand: false
        },

        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: onClick
        }
    };
    var data = @MvcHtmlString.Create(Model.LeftTree.ToJson());
    var zNodes = data;

    function onClick(e, treeId, treeNode) {
        if(treeNode.raw==null){
            return;
        }
        $("#assemblyQualifiedName").val(treeNode.raw.FullName);
        $("#masterDataCreate").attr("href","@Url.Action("Create")/?assemblyQualifiedName="+escape($("#assemblyQualifiedName").val()));
        $("#masterDataCreate").attr("onclick", "");
        $("#masterDataCreate").attr("data-toggle","ajaxModal");

        var table= $('#example').DataTable();
        //table.ajax.url("/MasterData/GetPaged?assemblyQualifiedName="+ escape($("#assemblyQualifiedName").val()));
        table.ajax.reload();

    }
    $(document).ready(function(){
        $.fn.zTree.init($("#tree"), setting, zNodes);

        LoadData();
    });


    function LoadData() {
        var table = $('#example').dataTable({
            serverSide: true,
            sort: false,
            displayLength: 14,
            ajax: {
                "url": "@Url.Action("GetPaged")",
                "type": "POST"
            },
            serverParams: function (aoData) {
                eval("aoData.assemblyQualifiedName='" + $("#assemblyQualifiedName").val() + "'");
            },
            columns: [
                { title: '<label class="checkbox m-l-12 m-t-none m-b-none i-checks"><input type="checkbox" /><i></i></label>', data: function (row, type, set) { return '<label class="checkbox m-l-12 m-t-none m-b-none i-checks"><input type="checkbox" name="cellCheckbox" value="' + row.Id + '" /><i></i></label>'; }, "bStorable": false, width: 30 },
                { title: "序号", data:"LineNumber", width: 40, "sClass": "right" },
                { title: "代码", data: 'Code', width: 120, "sClass": "center" },
                { title: "名称", width:160, data: function (row) { return '<a data-toggle="ajaxModal" class="auto" href="@Url.Action("Edit")/?id=' + row.Id + '&assemblyQualifiedName='+ escape( $("#assemblyQualifiedName").val())+'">'+row.Name+'</a>'; }
                },
                { title: "全称", data: 'FullName' }

            ]
        });
    }

    function DeleteMasterData(dataTable, url) {

        if ($("#" + dataTable).length === 0) {
            window.app.message("实现错误，列表Id给错。");
            return;
        }
        var ids = new Array();

        $("#" + dataTable + " tbody tr").each(function (x) {

            var checkbox = $(this).find("td:eq(0) input");

            if (checkbox.length > 0 && checkbox.prop('checked')) {
                ids.push(checkbox.val());
            }
        });

        if (ids.length === 0) {
            window.app.message("请选中一项。");
            return;
        }

        swal({
            title: "请确认是否删除？",
            text: "您正在进行删除操作，该操作不可逆!",
            type: "warning",
            showCancelButton: true,
            confirmButtonClass: "btn-danger"
        }).then(function(isConfirm) {
            if (isConfirm === true){
                $.ajax({
                    type: "POST",
                    url: url,
                    data: { 
                        "ids": ids,
                        assemblyQualifiedName : $("#assemblyQualifiedName").val() 
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.errors.length > 0) {
                            swal("出错啦！", data.errors, "error");
                        }
                        else {
                            swal("已经删除", "你已经删除成功！", "success");
                            $('#example').DataTable().ajax.reload();
                        }
                    }
                });
            }
        });
    }
</script>